package game.components.squares;

import game.components.Cordinent;
import game.components.Player;
import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Composite;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Stroke;
import java.awt.event.MouseListener;
import java.awt.geom.GeneralPath;
import ui.ZigZagStroke;

public class GamePiece extends BasicSquare implements Comparable<GamePiece>
{
	private static final long	serialVersionUID	= 7985981324798791508L;
	private boolean				selected;
	private Player				owner;
	private boolean				showValue;
	private int					value;

	public GamePiece(Cordinent location, Player player, int value)
	{
		this(null, location, player, value, false);
	}

	public GamePiece(MouseListener l, Cordinent location, Player player,
			int value)
	{
		this(l, location, player, value, false);
	}

	public GamePiece(MouseListener l, Cordinent location, Player player,
			int value, boolean showValue)
	{
		super(l, location);
		this.owner = player;
		this.value = value;
		this.showValue = showValue;
		this.highlighted = false;

		if (this.value != 0 && this.value != 11)
		{
			this.movable = true;
		}
		else
		{
			this.movable = false;
		}
		if (this.value == 2)
		{
			this.movableMultipleSquares = true;
		}
		else
		{
			this.movableMultipleSquares = false;
		}
	}

	@Override
	public int compareTo(GamePiece o)
	{
		if (o == null){ return 0; }
		if (!( o instanceof GamePiece ))
		{
			return 0;
		}
		else
		{
			int oValue = o.getValue();
			int thisValue = this.getValue();

			// Special Cases

			// Bombs and 3's
			if (oValue == 3 || thisValue == 3)
			{
				if (oValue == 3 && thisValue == 11){ return -1; }
				if (oValue == 11 && thisValue == 3){ return 1; }
			}

			// 10's and Spys
			if (oValue == 1 || thisValue == 1)
			{
				if (oValue == 1 && thisValue == 10){ return -1; }
				if (oValue == 10 && thisValue == 1){ return 1; }
			}

			// Default Case
			if (thisValue - oValue == 0)
			{
				return 0;
			}
			else if (thisValue - oValue > 0)
			{
				return 1;
			}
			else
			{
				return -1;
			}
		}

	}

	public Color getColor()
	{
		return this.owner.getColor();
	}

	public boolean getSelected()
	{
		return this.selected;
	}

	public boolean getShowValue()
	{
		return this.showValue;
	}

	public int getValue()
	{
		return this.value;
	}

	@Override
	public void highlight(Graphics2D g2d)
	{
		if (g2d != null)
		{
			Composite oldComp = g2d.getComposite();
			Composite alphaComp = AlphaComposite.getInstance(
					AlphaComposite.SRC_OVER, 0.5f);
			g2d.setComposite(alphaComp);
			if (this.selected)
			{
				g2d.setColor(Color.black);
			}
			else
			{
				g2d.setComposite(alphaComp);

				g2d.setColor(Color.white);

			}
			g2d.setStroke(new BasicStroke(8));

			GeneralPath p = new GeneralPath();
			p.moveTo(4, 4);
			p.lineTo(this.getWidth() - 4, 4);
			p.lineTo(this.getWidth() - 4, this.getHeight() - 4);
			p.lineTo(4, this.getHeight() - 4);
			p.closePath();
			g2d.draw(p);

			g2d.setComposite(oldComp);
			g2d.setStroke(new BasicStroke(1));
		}

	}

	@Override
	public void paintComponent(Graphics g)
	{
		Graphics2D g2d = BasicSquare.getAntiAliasedGraphics(g);
		g2d.setColor(this.owner.getColor());

		// DRAW BORDER

		Stroke basicStroke = new BasicStroke(8);
		Stroke zigZagStroke = new ZigZagStroke(basicStroke, 4, 16);

		GeneralPath p = new GeneralPath();
		p.moveTo(4, 4);
		p.lineTo(this.getWidth() - 4, 4);
		p.lineTo(this.getWidth() - 4, this.getHeight() - 4);
		p.lineTo(4, this.getHeight() - 4);
		p.closePath();

		g2d.setStroke(zigZagStroke);
		g2d.draw(p);
		g2d.setStroke(basicStroke);
		g2d.draw(p);

		// END DRAW BORDER

		// DRAW PICE VALUE

		// if (!this.showValue)
		// {
		// g.setFont(new Font("SansSerif", Font.BOLD, 24));
		// g.drawString("?", 27, 45);
		// }
		// else
		{
			// g2d.setStroke(new BasicStroke(1));
			// g2d.drawLine(35, 0, 35, 70);
			// g2d.drawLine(0, 35, 70, 35);
			g.setFont(new Font("SansSerif", Font.BOLD, 20));
			if (value == 0)// FLAG
			{
				g2d.setStroke(new BasicStroke(2));
				g2d.fillRect(20, 20, 30, 20);
				g2d.drawLine(20, 20, 20, 55);
				g.setColor(Color.white);
				g.drawString("F", 30, 37);
				g2d.setStroke(new BasicStroke(1));
				g2d.setColor(this.owner.getColor());
			}
			else if (value == 1)// SPY
			{
				g.drawString("Spy", 17, 42);
			}
			else if (value == 11)// BOMB
			{
				g2d.fillOval(22, 25, 25, 25);
				g2d.setStroke(new BasicStroke(2));
				g2d.drawArc(35, 18, 20, 20, 90, 180);
				g2d.setStroke(new BasicStroke(1));
			}
			else if (value > 9)
			{
				g.drawString(Integer.toString(value), 24, 42);
			}
			else
			{
				g.drawString(Integer.toString(value), 30, 42);
			}
		}

		g2d.setStroke(new BasicStroke(1));
		super.drawBorder(g2d);

		if (this.highlighted)
		{
			this.highlight(g2d);
		}

	}

	public void setSelected(boolean selected)
	{
		this.selected = selected;
	}

}
